<?
/* 		
 *	SOCIAL: TWITTER
 *	
 *	For now -> update / delete twitter status with twitter api (for newsposts)
 *  Very expandable
 *
 * 	@author 	oneDot only
 *  @date 		15/06/2010
 */



class TWITTER extends OBJECT {

	function __construct() {
		global $DB;
		
		$this->type = "social.twitter";
		
		if( ! $this->id = (int) $DB->get_key( $this->type, "active",  "ID" ))
			return (object) array();
		
#		Load assigned object or most recent
		$this->load( $this->type, $this->id = (int) $DB->get_key( $this->type, "active",  "ID" ));

	}
	
	
	// Connect to external class and authenticate
	function initialize() {
		global $DB, $Engine;
		
		require_once ROOT . 'engine/ext/twitter/twitter.php';
	
		// setup connection with callback url
		$twitter = new Tweet(json_decode($DB->get_key("social.twitter", "active", "meta"))->api, json_decode($DB->get_key("social.twitter", "active", "meta"))->secret);
		
		// authorize
		$twitter->setOAuthToken(json_decode($DB->get_key("social.twitter", "active", "meta"))->oauth_token);
		$twitter->setOAuthTokenSecret(json_decode($DB->get_key("social.twitter", "active", "meta"))->oauth_token_secret);
				
		return $twitter;
	}

	
	function reference_post( $objtype, $objid = false ) {
		global $DB;
		
		return $DB->reference( $this->type, $this->id, $objtype, $objid, "social" );
	}
	

	function publish() {
		global $DB, $Request;
		
		$objid = (int) $Request->vars->objid;
		
		// get news post		
		$long_key = "<%lang:contid:long%>";
		$nodes = $DB->filter_key("news", "ID = $objid limit 1", array( "<%lang:nameid%> as name", "$long_key as content") );

		foreach ($nodes as $node) {
			$content 	= $node['name'] . ': ' . $node['content'];
			if (strlen($content) > 99) 
				$tweet = substr($content, 0, 100) . '... ';
			else
				$tweet = $content . '... ';
		}
		
		// shorten url
		$url = str_replace('local', 'be', 'http://' . $_SERVER['SERVER_NAME']);
		$url = str_replace('engine.', '', $url);
		$url = file_get_contents('http://tinyurl.com/api-create.php?url=' . $url );
		
		// tweet
		preg_match_all( "/<a[^>]*href=\\\"([^\\\"]*)\\\"[^>]*>[^<]*".$a."[^<]*<\\/a>/miU", $tweet, $tweetie );
		$tweet = strip_tags( str_replace( $tweetie[0], $tweetie[1], $tweet )) . $url;
		$twitter = $this->initialize();
		
		$info = $twitter->statusesUpdate( $tweet );	

		// store in glue_social & update meta in mod_news
		$DB->set_reference ( $Request->type, $this->id, $Request->vars->objtype, $objid, "social" );
		
		// update meta
		$meta	= json_encode( array( "tweetid" => $info['id'] ));
		$DB->query( "update ". $DB->type( "news", "table" ) ." set meta = '" . $meta . "' where ID = " . $objid . " limit 1", 0 );
		//$DB->update( "news", $objid, array( "active" => 1, "meta"=> array('tweetid' => $info['id'] )));

		// engine message
		return "Tweeted!";
	}


	function unpublish() {
		global $DB, $Request;
		
		$objid = (int) $Request->vars->objid;
		
		// get id from twitter by mod_news table
		$tweetid = json_decode($DB->get_key("news", $objid, "meta"))->tweetid;
	
		// delete from glue_social & empty meta in mod_news table
		$DB->delete_reference( $Request->type, 0, 0, $objid );
		$DB->query( "update ". $DB->type( "news", "table" ) ." set meta = '' where ID = " . $objid . " limit 1", 0 );
		//$DB->update ( "news", $objid, array( "meta"=> array( "tweetid"=>"" )));
			
		// delete from twitter
		$twitter = $this->initialize();
		$info = $twitter->statusesDestroy( $tweetid );
		
		// engine message
		return "Untweeted!";
	}

}

?>